# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=shaderc
pkgver=2022.1
pkgrel=2
pkgdesc="A collection of tools, libraries, and tests for Vulkan shader compilation"
options="!check" # Requires test setup from third party
url="https://github.com/google/shaderc"
arch="all"
license="Apache-2.0"
makedepends="
	cmake
	glslang-dev
	glslang-static
	python3
	samurai
	spirv-tools-dev
	"
subpackages="$pkgname-static $pkgname-dev"
source="$pkgname-$pkgver.tar.gz::https://github.com/google/shaderc/archive/v$pkgver.tar.gz"

prepare() {
	default_prepare

	# Disable third party dependencies
	sed -i -e '/add_subdirectory(third_party)/d' CMakeLists.txt

	# Fix glslc test generation depending on third-party SPIRV-tools
	sed -i -e "s|\$<TARGET_FILE:spirv-dis>|spirv-dis|" \
		glslc/test/CMakeLists.txt

	# Disable git versioning
	sed -i -e '/build-version/d' glslc/CMakeLists.txt

	# Create our own build-version.inc since we disabled git versioning
	# need to keep this in sync with glslang and SPIRV-Tools versions
	# this is displayed with 'glslc --version'
	_glslang_ver="$( . $startdir/../../*/glslang/APKBUILD; echo $pkgver)"
	_spirv_ver="$( . $startdir/../../*/spirv-tools/APKBUILD; echo $pkgver)"

	cat <<- EOF > glslc/src/build-version.inc
		"shaderc $pkgver\n"
		"SPIRV-Tools $_spirv_ver\n"
		"glslang $_glslang_ver\n"
	EOF
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DBUILD_SHARED_LIBS=ON \
		-DSHADERC_SKIP_TESTS="$(want_check && echo OFF || echo ON)" \
		-DSHADERC_ENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DSHADERC_ENABLE_EXAMPLES=OFF \
		-Dglslang_SOURCE_DIR=/usr/include/glslang \
		-DSPIRV-Headers_SOURCE_DIR=/usr \
		-Dspirv-tools_SOURCE_DIR=/usr \
		$CMAKE_CROSSOPTS
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
21c2462cb434d94da87c71ba660f6cd5b161450d7faeff8789b41db25e64f2baa7c560e78aa856d128c5a73699215d6b1085b5ca19a4640237adf194793ad44b  shaderc-2022.1.tar.gz
"
